//
//  MineMessageCollectionView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/9.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class MineMessageCollectionView: UIView,UITableViewDelegate,UITableViewDataSource {
    var table : RefreshedTableView?
    private var source : Array<MineMessageModel> = []
    init(frame: CGRect,delegate : beginRefresh) {
        super.init(frame: frame)
        table = RefreshedTableView.init(frame: CGRect.init(x: 0, y: 0, width: YXDeviceWidth, height: YXDeviceHeight - 64), style: .plain)
        table!.delegate = self
        table!.dataSource = self
        table!.allowsSelection = false
        table!.separatorStyle = .none
        table!.backgroundColor = UIColor.colorWithHexString(hex: "f3f3f5")
        table!.refreshDelegate = delegate
        table!.register(MineMessageTableViewCell.classForCoder(), forCellReuseIdentifier: "mineMessage")
        table!.addFooter()
        self.addSubview(table!)
    }
    
    func feedData(model : MineCollectionMessageModel) -> Void {
        source = model.sourceArr
        table!.reloadData()
        table!.smileView.endRefresh()
    }
    
    func appendData(model : MineCollectionMessageModel) -> Void {
        source.append(contentsOf: model.sourceArr)
        table!.reloadData()
        table!.startAnimation = false
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return source.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 217 * YXHeightScale
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "mineMessage")!
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let cel = cell as! MineMessageTableViewCell
        cel.model = source[indexPath.row]
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offY = scrollView.contentOffset.y
        if offY <= 0 {
            table!.smileView.scrollDidScroll()
        }
    }
    
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        table!.smileView.scrollDidEndDraging()
        let queue = DispatchQueue.init(label: "Global")
        queue.async {
            if self.table!.contentSize.height <= self.table!.frame.size.height {
                if self.table!.contentOffset.y > 20 {
                    self.table!.startAnimation = true
                    self.table!.refreshDelegate.loadNext!()
                }
            }else{
                if self.table!.contentOffset.y > (self.table!.contentSize.height-self.table!.frame.size.height+20) {
                    //加载下一页
                    self.table!.refreshDelegate.loadNext!()
                }
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
